home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / oodb.zip / OODB.PAS < prev    next >
Pascal/Delphi Source File  |  1992-07-13  |  26KB  |  747 lines

  1. unit OODB;
  2.  
  3. interface
  4.  
  5.    uses Objects;
  6.  
  7.    const
  8.       PIDLimit: Word = $7FFF;
  9.       Delta = 4;
  10.       Hallmark = 9999;
  11.       IndexPointerLocation = 4;
  12.       StorageStart = 8;
  13.  
  14.    type
  15.  
  16.       { Record type for object registration }
  17.  
  18.       IndRec =
  19.          record
  20.             ID        : Word;
  21.             StartPos,
  22.             Size      : Longint;
  23.             Base      : Integer
  24.          end;
  25.       PIndRec = ^IndRec;
  26.  
  27.       { Stream for object size evaluation }
  28.  
  29.       TNullStream =
  30.          object (TStream)
  31.             SizeCounter : Longint;
  32.             constructor Init;
  33.             procedure   ResetCounter;                   virtual;
  34.             procedure   Write (var Buf; Count: Word);   virtual;
  35.             function    SizeInStream: Longint;          virtual;
  36.          end;
  37.       PNullStream = ^TNullStream;
  38.  
  39.       { Stream - database main storage }
  40.  
  41.       DBStream = TStream;
  42.       PDBStream = ^DBStream;
  43.  
  44.       { Collection for indexes }
  45.  
  46.       TIndexCollection =
  47.          object (TCollection)
  48.             procedure FreeItem (Item: Pointer);                 virtual;
  49.             function  GetItem (var S: TStream): Pointer;        virtual;
  50.             procedure PutItem (var S: TStream; Item: Pointer);  virtual;
  51.          end;
  52.       PIndexCollection = ^TIndexCollection;
  53.  
  54.       { --- TBASE - the main class --- }
  55.  
  56.       TBase =
  57.          object (TObject)
  58.  
  59.             BaseStream : PDBStream;         { Main storage pointer }
  60.             DBIndex,                        { Database index }
  61.             HolesIndex : PIndexCollection;  { Holes index }
  62.             PIDCurrent : Word;              { Unique identifier }
  63.             NS         : PNullStream;       { For object size evaluation }
  64.             DoneFlag   : Boolean;           { True if OODB is being disposed }
  65.  
  66.             function  BytesInStream (P: PObject): Longint ;
  67.                                virtual;
  68.             procedure IndexSort (Cat: PIndexCollection; StOrd: Boolean);
  69.                                virtual;
  70.             function  IndexFound (Cat: PIndexCollection;
  71.                                   LookFor: Longint;
  72.                                   var Pos: Integer;
  73.                                   PIDSorted: Boolean): Boolean;
  74.                                virtual;
  75.             function  HoleFound (S: Longint; var Pos: Longint): Boolean;
  76.                                virtual;
  77.  
  78.             procedure   Abort;                          virtual;
  79.             procedure   Commit;                         virtual;
  80.             constructor Init (AStream: PDBStream);
  81.             destructor  Done;                           virtual;
  82.             function    Create: Word;                   virtual;
  83.             procedure   Put (PID: Word; P: PObject);    virtual;
  84.             function    Get (PID: Word): PObject;       virtual;
  85.             procedure   Destroy (PID: Word);            virtual;
  86.  
  87.             function    ObjSize (PID: Word): Longint;   virtual;
  88.             function    Count: Integer;                 virtual;
  89.  
  90.             procedure   IdlePack;                       virtual;
  91.  
  92.          end; { -- TBase -- }
  93.       PBase = ^TBase;
  94.  
  95. implementation
  96.  
  97.    { -- Implementation of TNullStream -- }
  98.  
  99.    constructor TNullStream.Init;
  100.       begin
  101.          TStream.Init;
  102.          ResetCounter
  103.       end;
  104.  
  105.    procedure TNullStream.ResetCounter;
  106.       begin
  107.          SizeCounter := 0
  108.       end;
  109.  
  110.    procedure TNullStream.Write (var Buf; Count: Word);
  111.       { Overrides TStream.Write method }
  112.       begin
  113.          SizeCounter := SizeCounter + Count
  114.       end;
  115.  
  116.    function TNullStream.SizeInStream: Longint;
  117.       begin
  118.          SizeInStream := SizeCounter
  119.       end;
  120.  
  121.    { -- End of TNullStream implementation -- }
  122.  
  123.    { -- Implementation of TIndexCollection -- }
  124.  
  125.    procedure TIndexCollection.FreeItem (Item: Pointer);
  126.  
  127.       begin
  128.          Dispose (Item)
  129.       end;  { FreeItem }
  130.  
  131.    function TIndexCollection.GetItem (var S: TStream): Pointer;
  132.  
  133.       var Item : PIndRec;
  134.  
  135.       begin
  136.          New (Item);
  137.          with S do
  138.               with Item^ do
  139.                    begin
  140.                       Read (ID, SizeOf(ID));
  141.                       Read (StartPos, SizeOf(StartPos));
  142.                       Read (Size, SizeOf(Size));
  143.                       Read (Base, SizeOf(Base))
  144.                    end;
  145.          GetItem := Item
  146.       end;  { GetItem }
  147.  
  148.    procedure TIndexCollection.PutItem (var S: TStream; Item: Pointer);
  149.  
  150.       begin
  151.          with S do
  152.               with IndRec(Item^) do
  153.                    begin
  154.                       Write (ID, SizeOf(ID));
  155.                       Write (StartPos, SizeOf(StartPos));
  156.                       Write (Size, SizeOf(Size));
  157.                       Write (Base, SizeOf(Base))
  158.                    end
  159.       end;  { PutItem }
  160.  
  161.    { -- End of TIndexCollection implementation -- }
  162.  
  163.    { -- TBASE IMPLEMENTATION -- }
  164.  
  165.    { ----- BytesInStream ------------------------------------------ }
  166.  
  167.    function TBase.BytesInStream (P: PObject): Longint ;
  168.  
  169.    { Determines the number of bytes required
  170.      to put an object into the stream }
  171.  
  172.       begin
  173.          with NS^ do
  174.               begin
  175.                  ResetCounter;
  176.                  Put (P);
  177.                  BytesInStream := SizeInStream
  178.               end
  179.       end;
  180.  
  181.    { ----- IndexSort ---------------------------------------------- }
  182.  
  183.    procedure TBase.IndexSort (Cat: PIndexCollection; StOrd: Boolean);
  184.  
  185.    { Bubble-sorts any index (DBIndex or HolesIndex) according either to
  186.      StartPos'es in a stream (StOrd = True) or to PID's (StOrd = False) }
  187.  
  188.       var
  189.          i, j, k : Integer;
  190.          Min     : Longint;
  191.          Aux     : PIndRec;
  192.  
  193.       begin
  194.  
  195.          with Cat^ do
  196.  
  197.               for i := 0 to Count-2 do
  198.  
  199.                   begin
  200.                      if StOrd
  201.                         then begin
  202.                                 Min := IndRec(At(i)^).StartPos; k := i;
  203.                                 for j := i+1 to Count-1 do
  204.                                     if IndRec(At(j)^).StartPos < Min
  205.                                         then begin
  206.                                                 k := j;
  207.                                                 Min := IndRec(At(k)^).StartPos
  208.                                              end
  209.                              end
  210.                         else begin
  211.                                 Min := IndRec(At(i)^).ID; k := i;
  212.                                 for j := i+1 to Count-1 do
  213.                                     if IndRec(At(j)^).ID < Min
  214.                                        then begin
  215.                                                k := j;
  216.                                                Min := IndRec(At(k)^).ID
  217.                                             end
  218.                              end;
  219.                      Aux := At (i);
  220.                      AtPut (i,At(k)); AtPut (k,Aux)    { Bubble is up }
  221.                   end  { for }
  222.  
  223.       end; { IndexSort }
  224.  
  225.    { ----- IndexFound --------------------------------------------- }
  226.  
  227.    function TBase.IndexFound
  228.                   (Cat: PIndexCollection; LookFor: Longint;
  229.                    var Pos: Integer; PIDSorted: Boolean)    : Boolean;
  230.  
  231.    { Looks for LookFor in Cat^ index (binary search) and returns True
  232.      if hits it. Position for LookFor (Pos) is located by all means }
  233.  
  234.       var
  235.          m, j  : Integer;
  236.          Value : Longint;     { Value that is found }
  237.  
  238.       begin
  239.  
  240.          IndexFound := False;
  241.          with Cat^ do
  242.               begin
  243.                  Pos := 0; j := Count-1;
  244.                  if j < Pos
  245.                     then Exit;
  246.                  while j > Pos do
  247.                        begin
  248.                           m := ( Pos + j ) div 2;
  249.                           if ( PIDSorted and
  250.                                (IndRec(At(m)^).ID >= LookFor) )
  251.                              or
  252.                              ( not PIDSorted and
  253.                                (IndRec(At(m)^).StartPos >= LookFor) )
  254.